<?php
if (! defined ( 'BASEPATH' ))exit ( 'No direct script access allowed' );
//后台登陆
class Login extends CI_Controller{
  function __construct(){
   parent::__construct();
   $this->load->admin_Tpl_path();//它不继承 admin_controller 单独定义后台模板路径
   $this->load->helper('captcha');
   $this->load->library('session');
   $this->load->library('form_validation');
   $this->load->model('Users_model');
//   $this->output->enable_profiler(TRUE);
   
  }
  //登陆ui
  function index(){
  $this->load->view('login.html');
  }
  //验证码
  public  function captcha(){
   $captcha=create_captcha(4);
   $this->session->set_userdata('captcha',$captcha);
  }
  function captchaAjax(){
  echo $this->session->userdata('captcha');
  }
  //登陆提交处理
  function loginProcess(){
   $this->form_validation->set_rules('username','用户名','required');
   $this->form_validation->set_rules('password','密码','required');
   $verifycode=$this->input->post('verifycode');
   $captcha=$this->session->userdata('captcha');
     if(strtoupper($verifycode)==strtoupper($captcha)){
        if($this->form_validation->run()==false){
            $data['message']=validation_errors();
            $data['url']=site_url('admin/login/index');
            $data['wait']=3;
            $this->load->view('message.html',$data);
        }else{
             $username=$this->input->post('username',true);
             $password=$this->input->post('password',true);
              $where=array('u_name'=>$username,'pwd'=>md5($password));
                 $r=$this->Users_model->queryU($where);
	             if(!empty($r)){
	             	$this->Users_model->updateData(array('u_name'=>$username));
	               $person_info=$this->person_info($username);
	              $this->session->set_userdata($person_info);
	              $this->session->set_userdata('login',md5('w3a'));
	              redirect('admin/main/index');
	            }else{
	              $data=array(
                    'url'=>site_url('admin/login/index'),
                    'message'=>'用户名或密码错误，请重新填写',
                    'wait'=>3
                    );
                  $this->load->view('message.html',$data);
	             }
         }
    }else{
	     $data=array(
	     'url'=>site_url('admin/login/index'),
	     'message'=>'验证码错误，请重新填写',
	     'wait'=>3
	     );
	     $this->load->view('message.html',$data);
	    }
 }
 
 function logout(){
    $this->session->unset_userdata('login');
    $this->session->sess_destroy();
    redirect('admin/login/index');
 }
 
 protected function person_info($uname){
 	$this->load->model('Rbac');
    return $this->Rbac->getAll($uname);
    
 }
}


